package fhp.hlhj.giantfold.activity.malls

import android.annotation.TargetApi
import android.content.Intent
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.View
import com.flyco.tablayout.listener.OnTabSelectListener
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.interfaces.ISuperMarkt
import fhp.hlhj.giantfold.adapter.ViewPagerAdp
import fhp.hlhj.giantfold.fragment.AllGoodsFgm
import fhp.hlhj.giantfold.javaBean.ErroBean
import fhp.hlhj.giantfold.javaBean.TypesBean
import fhp.hlhj.giantfold.presenter.SuperMarketPresenter
import fhp.hlhj.giantfold.utils.LogUtil
import kotlinx.android.synthetic.main.activity_super_market_aty.*
import android.view.MotionEvent
import fhp.hlhj.giantfold.utils.MyUtils


class SuperMarketAty : BaseActivity(), ISuperMarkt {
    private lateinit var  pageAdpter : ViewPagerAdp
    private lateinit var  datas :Array<String?>
    private var up =true
    private  var fragments =ArrayList<Fragment>()
    override fun getContentId(): Int = R.layout.activity_super_market_aty
    private lateinit var superMaketPresenter:SuperMarketPresenter
    override fun initView() {
        superMaketPresenter= SuperMarketPresenter(this)
        superMaketPresenter.getTypes()
    }
    /**
     *获取类型列表
     */
    @RequiresApi(Build.VERSION_CODES.M)
    override fun getTypes(typesBean: TypesBean) {
        datas = arrayOfNulls<String>(typesBean.data.size)
        for ((i, cate) in typesBean.data.withIndex()){
            LogUtil.log("获得类型有"+cate.classname)
            datas[i] = cate.classname
        }
        for (i in 0 until datas.size){
            val fgm = AllGoodsFgm.newInstece(typesBean.data[i],i)
            fragments.add(fgm)
        }
        pageAdpter = ViewPagerAdp(supportFragmentManager,fragments)
        viewPager.adapter=pageAdpter
        viewPager.offscreenPageLimit=1
        viewPager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(p0: Int) {
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
            }

            override fun onPageSelected(p0: Int) {
                viewPager.resetHeight(p0)
            }
        })
        tabLayout.setViewPager(viewPager,datas)
        tabLayout.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            if (up){
                tabLayout2.smoothScrollTo(scrollX,0)
            }
        }
        tabLayout2.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            if (!up){
                tabLayout.smoothScrollTo(scrollX,0)
            }
        }
        tabLayout2.setViewPager(viewPager,datas)
    }
    override fun showLoading() {
        LogUtil.log("开始获得类型")
    }

    override fun onError(erro: ErroBean) {
        if (erro!=null){
            LogUtil.log("错误信息"+erro.error.message.toString())
        }
    }
    @TargetApi(Build.VERSION_CODES.M)
    override fun initListener() {
        btSearch.setOnClickListener {
            startActivity(Intent(this,SearchGoodAty::class.java))
        }
        btExit.setOnClickListener {
            finish()
        }
        tabLayout.setOnTabSelectListener(object : OnTabSelectListener{
            override fun onTabSelect(position: Int) {
            }
            override fun onTabReselect(position: Int) {
            }
        })
        /*Scroll监听*/
        scrollView.setOnScrollChangeListener {
            v, scrollX, scrollY, oldScrollX, oldScrollY ->
            if (scrollY>=tabLayout.top){
                tabLayout2.visibility=View.VISIBLE
//                radioLo.visibility=View.VISIBLE
            }else{
                tabLayout2.visibility=View.GONE
//                radioLo.visibility=View.GONE
            }
        }
        scrollView.setOnTouchListener { _, event ->
            // 监听滑动到头部与底部
            when (event.getAction()) {
                MotionEvent.ACTION_MOVE -> {
                    /**
                     * 1.getScrollY()获取滑动的距离 <br></br>
                     * 2.getHeight()获取在屏幕上显示的高度 <br></br>
                     * 3.getMeasureHeight()获取控件的真实高度(包含屏幕外的高度) <br></br>
                     */
                    /**
                     * 1.getScrollY()获取滑动的距离 <br></br>
                     * 2.getHeight()获取在屏幕上显示的高度 <br></br>
                     * 3.getMeasureHeight()获取控件的真实高度(包含屏幕外的高度) <br></br>
                     */
                    if (scrollView.scrollY <= 0) {
                        // 说明没有滑动，在屏幕顶部
                        println("已经滑动到屏幕顶部")
                    }

                    // TODO: 获取ScrollView中包裹的View的高度！！！
                    val measuredHeight = scrollView.getChildAt(0)
                            .measuredHeight
                    val scrollY = scrollView.scrollY
                    val height = scrollView.height
                    if (measuredHeight <= scrollY + height) {
                        // 已经滑动的距离+在屏幕上显示的高度>=控件真实高度。说明已经滑动到底部
                        println("已经滑动到底部")
                        println("measuredHeight=" + measuredHeight
                                + " scrollY= " + scrollY + " height= "
                                + height)
                        val sb = StringBuilder()
                        try {
                            val fgm = fragments[viewPager.currentItem] as AllGoodsFgm
                            fgm.nextPage()
                        }catch (e:Exception){

                        }

                    }
                }
                else -> {
                }
            }
            false
        }

    }
}
